<article class="" id="post-">
<header class="entry-header">
<h1 class="entry-title">.ajaxStart()</h1>
<div class="entry-meta">
						所属分类：<span class="category"><a href="/category/ajax/">Ajax</a> &gt; <a href="/category/ajax/global-ajax-event-handlers/">全局 Ajax 事件处理器</a></span>
<span class="pull-right">英文文档：<a href="https://api.jquery123.com/ajaxStart/" target="_blank">.ajaxStart()</a></span>
</div><!-- .entry-meta -->
</header><!-- .entry-header -->
<!-- .entry-header -->
<div class="entry-content">
<article class="entry method" id="ajaxStart1"><h2 class="section-title">
<span class="name">.ajaxStart( handler() )</span><span class="returns">返回: <a href="/Types/#jQuery">jQuery</a></span>
</h2>
<div class="entry-wrapper">
<p class="desc"><strong>描述: </strong>在AJAX 请求刚开始时执行一个处理函数。 这是一个 <a href="/Ajax_Events">Ajax Event</a>.</p>
<ul class="signatures"><li class="signature" id="ajaxStart-handler">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.0/">1.0</a></span>.ajaxStart( handler() )</h4>
<ul><li>
<div><strong>handler()</strong></div>
<div>类型: <a href="/Types/#Function">Function</a>()</div>
<div>被调用的函数。</div>
</li></ul>
</li></ul>
<div class="longdesc" id="entry-longdesc">
<p>每当一个Ajax请求即将发送，jQuery检查是否有任何其他响应过程中的Ajax请求（注：未完成的请求）。如果没有检查到，jQuery就会触发<code>ajaxStart</code>事件，在这个时间点所有处理函数都会使用<code>.ajaxStart()</code>方法注册并执行。</p>
<p>观察这种方法，建立一个基本的Ajax加载请求：</p>
<div class="syntaxhighlighter xml nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">div</span> <span class="attribute">class</span>=<span class="value">"trigger"</span>&gt;</span>Trigger<span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">div</span> <span class="attribute">class</span>=<span class="value">"result"</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">div</span> <span class="attribute">class</span>=<span class="value">"log"</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>在document元素上绑定事件处理器：</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$(document).ajaxStart(<span class="keyword">function</span>() {</code></div></div><div class="container"><div class="line"><code>  $( <span class="string">".log"</span> ).text( <span class="string">"Triggered ajaxStart handler."</span> );</code></div></div><div class="container"><div class="line"><code>});</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>现在，我们可以使用任何的jQuery方法构建一个Ajax请求：</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$( <span class="string">".trigger"</span> ).click(<span class="keyword">function</span>() {</code></div></div><div class="container"><div class="line"><code>  $( <span class="string">".result"</span> ).load(<span class="string">"ajax/test.html"</span>);</code></div></div><div class="container"><div class="line"><code>});</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>当我们点击class为 <code>trigger</code> 的元素并且Ajax请求发送，这个信息就会显示。
			            </p>
<p><strong>但是从 jQuery 1.8 开始, <code>.ajaxStart()</code> 方法只能绑定到 <code>document</code>元素.</strong></p>
</div>
<h3>其他注意事项：</h3>
<div class="longdesc"><ul>
<li> 
			                如果<code>global</code>选项设置为<code>false</code>，调用<code><a href="/jQuery.Ajax">$.ajax()</a></code> 或 <code><a href="/jQuery.ajaxStart">.ajaxStart()</a></code>方法将不会被触发。</li>
</ul></div>
<section class="entry-examples" id="entry-examples"><header><h2 class="underline">例子:</h2></header><div class="entry-example" id="example-0">
<h4><span class="desc">当Ajax请求开始发送时显示一个信息 (没有一个Ajax请求是已经激活的).</span></h4>
<div class="syntaxhighlighter javascript ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$(document).ajaxStart(<span class="keyword">function</span>() {</code></div></div><div class="container"><div class="line"><code>   $( <span class="string">"#loading"</span> ).show();</code></div></div><div class="container"><div class="line"><code> });</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
</div></section>
</div></article> </div>
</article>